﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;

namespace SysManage.Module.TalentWorld
{
    public partial class TalentWorldManageEdit : PageBase
    {
        #region 页面属性
        public string Category
        {
            get
            {
                return this.Request["category"] ?? "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                cbStatus.Checked = article.Status == 4;
                cbFlags.Checked = article.Flags == 1;
                heContent.Text = article.Content;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.Title = txtTitle.Text.Trim();
                article.Status = cbStatus.Checked ? 4 : 1;
                article.Flags = cbFlags.Checked ? 1 : 0;
                article.Content = heContent.Text;
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;
                
                DB.SaveChanges();
            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.Title = txtTitle.Text.Trim();
                article.Status = cbStatus.Checked ? 4 : 1;
                article.Flags = cbFlags.Checked ? 1 : 0;
                article.Content = heContent.Text;
                article.Module = "TalentWorld";
                article.Category = this.Category;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;

                DB.PartyArticles.Add(article);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}